/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package function;

import java.util.Calendar;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import mainpackage.Constant;
import mainpackage.GameBean;
import mainpackage.MainCanvas;
import mainpackage.MainLogic;
import mainpackage.MainView;
import mainpackage.ResManager;
import mainpackage.Tools;

/**
 *
 * @author Administrator
 */
public class QuickOption {

    private static int iWidth;
    private static int iHeight;
    private String sMess, sTitle;
    private int iOffsetX;
    private int iOffsetY;
    private int iStartX;
    private int iStartY;
    private Image[] imgArray;
    //是否显示
    public static int iShowQuickNote = -1;
    public static Vector contentV = new Vector(20, 8);
    public static int iSelectIndex, iStartIndex, iEndIndex;
    public static int iSelectMaxIndex = 5;
    public static int iCountIndex = 0;
    public static boolean blShowQuickNote = false;
    private static Image imgQuickNote = null;
    public static String strQuickNote = null;
    //绘制快捷提示人物提示

    public QuickOption() {
    }

    public static void setQuickNote(boolean blnote) {
        if (blnote) {
            //#if NOKIA_S40V3 || NORMAL_240_400 || MOTO || K790 
            //#else
            if (imgQuickNote == null) {
                imgQuickNote = ResManager.getImage(Constant.RES_UI_PATH + "90000115.png", 9999);
            }
            blShowQuickNote = true;
            POS_X2 = Constant.SCREEN_WIDTH - 74;
            POS_Y2 = Constant.SCREEN_HEIGHT - imgQuickNote.getHeight() - 29 + 10;
            //#if NOKIA_S60V5
//#             strQuickNote = "点击有新提示";
            //#else
            strQuickNote = "有新提示";
//            strQuickNote = "按#键有新提示";//谢亮
            //#endif
            //#endif

            iWidth = 114;
            iHeight = 80;
        } else {
            imgQuickNote = null;
            blShowQuickNote = false;
        }
    }

    public static void drawQuickNote(Graphics g) {
        if (!blShowQuickNote) {
            return;
        }
        iCountIndex += 1;
        if (iCountIndex > 1000) {
            iCountIndex = 0;
        }
        //#if NOKIA_S40V3 || NORMAL_240_400 || MOTO || K790
        //#else
        if (imgQuickNote != null) {
            g.setClip(POS_X2, POS_Y2, 76, 84);//头
            g.drawImage(imgQuickNote, POS_X2, POS_Y2 - 10, 0);
            g.setClip(POS_X2 + 22, POS_Y2 + 59, 26, 10);//眼睛
            g.drawImage(imgQuickNote, POS_X2 - (((iCountIndex % 30) == 0) ? 0 : 27) + 22, POS_Y2 + 59, 0);
            g.setClip(POS_X2 - 11, POS_Y2 + 84, 45, 52);//左边身子
            g.drawImage(imgQuickNote, POS_X2 - 11, POS_Y2 - 11, 0);
            g.setClip(POS_X2 - 11 + 45, POS_Y2 + 84, 6, 52);//中间身子
            g.drawImage(imgQuickNote, POS_X2 - 11 - 1, POS_Y2 - 11, 0);
            g.setClip(POS_X2 - 11 + 45 + 6, POS_Y2 + 84, 45, 52);//右边身子
            g.drawRegion(imgQuickNote, 0, 95, 45, 52, Constant.TRANS_MIRROR, POS_X2 - 11 + 45 + 6, POS_Y2 + 84, 0); //右上角
        }
        //#endif
        //#if NOKIA_S60V5
         Tools.drawTextArea(g, strQuickNote, 0x000000, 0x000000, POS_X2 - Constant.FONT_SMALL.stringWidth(strQuickNote) - 12, POS_Y2 + 45, Constant.FONT_SMALL.stringWidth(strQuickNote) + 12, 30, 0xfbffe8, 0x847065);
        //#else
//#        Tools.drawTextArea(g, strQuickNote, 0xffffff, 0x000000, POS_X2 - Constant.FONT_SMALL.stringWidth(strQuickNote) - 12, POS_Y2 + 45, Constant.FONT_SMALL.stringWidth(strQuickNote) + 12, 30, 0xfbffe8, 0x847065);
        //#endif
    }

    public void initQuickOption() {
        iStartIndex = 0;
        iEndIndex = iSelectMaxIndex - 1;
        //#if NOKIA_S60V5
         this.iWidth = 310;
         this.iHeight = 120;
        //#else
//#        this.iWidth = 200;
//#        this.iHeight = 230;
        //#endif
        if (this.sMess != null && this.sMess.length() > 0) {
            int iTextHeight = Tools.getParagraphHeight(sMess, this.iWidth - 20, Constant.FONT_SMALL_HEIGHT);
            if (iTextHeight > 50) {
                this.iHeight = this.iHeight + (iTextHeight - 50);
            }
        }
        this.iStartX = Constant.UI_START_X + ((Constant.SCREEN_WIDTH - this.iWidth) >> 1) + iOffsetX;
        //#if NOKIA_S60V5
         this.iStartY = Constant.UI_START_X + ((Constant.END_LINE - this.iHeight) >> 1) + iOffsetY;
        //#else
//#        this.iStartY = Constant.UI_START_X + ((Constant.SCREEN_HEIGHT - this.iHeight) >> 1) + iOffsetY;
        //#endif
        try {
            //弹出层的图片数组
            //#if NOKIA_S60V5
             switch (MainLogic.getInstance().byGameState) {
                 case Constant.GAME_UI_ROAM:
                     this.imgArray = new Image[7];
                     this.imgArray[3] = ResManager.createImage(90000043);
                     this.imgArray[4] = ResManager.createImage(90000086);
                     this.imgArray[5] = ResManager.createImage(90000090);
                     this.imgArray[6] = Tools.makeTransparent(Constant.SCREEN_WIDTH, 10, 100, 0x252525);//半透明背景图片
                     break;
                 default:
                     this.imgArray = new Image[7];
                     this.imgArray[3] = ResManager.createImage(90000043);
                     this.imgArray[4] = ResManager.createImage(90000086);
                     this.imgArray[5] = ResManager.createImage(90000090);
                     this.imgArray[6] = Tools.makeTransparent(Constant.SCREEN_WIDTH, 10, 100, 0x252525);//半透明背景图片
                     break;
             }
            //#else
//#            this.imgArray = new Image[3];
            //#endif
            this.imgArray[0] = Tools.makeTransparent(ResManager.createImage(90000051), 220);
            //透明度
            this.imgArray[1] = Tools.makeTransparent(10, this.iHeight + this.iHeight / 10, 220, 0x989898);
            this.imgArray[2] = ResManager.createImage(90000044, Constant.RES_PUBLIC_TEMP, Constant.RES_UI_PATH, false, -1);
            this.sTitle = "快捷提示";
        } catch (Exception e) {
            if (Constant.DEBUG) {
                e.printStackTrace();
            }
        }
    }

    public void addQuickOption(String note, boolean changetype) {
        if (changetype) {
            iShowQuickNote = 1;
            setQuickNote(true);
        }
        int index = -1;
        if (contentV != null) {
            index = checkIfHaveC(note);
            if (index == -1) {
                contentV.addElement(note);
            } else {
                contentV.setElementAt(note, index);
            }
        }

    }

    public void deleteQuickOption(int index) {
        if (contentV != null && index < contentV.size()) {
            contentV.removeElementAt(index);
        }
    }

    public void drawQuickOption(Graphics g) {
        if (imgArray[0] != null) {
            //#if NOKIA_S60V5
             Tools.drawTilePic(g, (byte) 2, Constant.UI_START_X, Constant.UI_START_Y, Constant.DRAW_HEIGHT, imgArray[6], 0, 0, Constant.DRAW_WIDTH,
                     imgArray[6].getHeight(), Constant.TRANS_NONE);
             Tools.drawBottom(g, imgArray[3], imgArray[5], imgArray[4]);
            //#endif
            Tools.drawPopUp(g, (byte) 4, this.iStartX, this.iStartY, this.iWidth, this.iHeight, imgArray[0], imgArray[1], (byte) 1, imgArray[2], 43, 0, 21, 10, imgArray[2], 64, 0, 22, 10, this.sTitle, this.sMess);
            String tempString = null;
            for (int i = 0; i < contentV.size(); i++) {
                tempString = getNetKey((String) contentV.elementAt(i), 1);
//            g.drawString(tempString, this.iStartX + (iWidth - Constant.FONT_SMALL.stringWidth(tempString)) >> 1, this.iStartY + 36 + 30*i, 0);
                Tools.drawShadowString(g, (byte) 1, tempString, this.iStartX + ((iWidth - Constant.FONT_SMALL.stringWidth(tempString)) >> 1),
                        this.iStartY + 36 + 30 * i, iSelectIndex == i ? 0x00CCFF : 0xFFFFFF, 0x000000);
            }
            tempString = null;
            MainView.drawTouchRect(g, quickOptionrect);
        }
    }
    private static int[][] quickOptionrect;
    public void checkQuickOption(int x, int y) {
//    	if (quickOptionrect == null) {
    		quickOptionrect = new int[contentV.size()][];
//    	}
    	String tempString = null;
    	for (int i = 0; i < contentV.size(); i++) {
    		tempString = getNetKey((String) contentV.elementAt(i), 1);
    		quickOptionrect[i] = new int[]{this.iStartX + ((iWidth - Constant.FONT_SMALL.stringWidth(tempString)) >> 1),
                    this.iStartY + 36 + 30 * i,360,30};
    	}
    	int touched = MainCanvas.checkTouch(x, y, quickOptionrect);
    	if (touched >= 0) {
    		if (touched == iSelectIndex) {
    			MainCanvas.pressFire();
    		} else {
    			iSelectIndex = touched;
    		}
    	} else {
    		MainCanvas.pressBack();
    	}
    }

    public String getNetKey(String note, int type) {
        int index = note.indexOf("/");
        switch (type) {
            case 1:
                return note.substring(index + 1);
            case 2:
                if (note.indexOf("|") != -1) {
                    index = note.indexOf("|");
                }
                return note.substring(0, index);
        }
        return null;
    }

    public static String getNetKeyValue(String strContent, int nums) {
        int iNetKeyNums = 0;
        int length = strContent.length();
        char tempChar;
        for (int i = 0; i < length; i++) {
            tempChar = strContent.charAt(i);
            if (tempChar == '|') {
                {
//                strContent =  strContent.substring(i+1);
                    for (int j = i + 1; j < length; j++) {
                        tempChar = strContent.charAt(j);
                        if (tempChar == '|') {
                            //没有数据跳过
                            if (j == i + 1) {
                                break;
                            }
                            if (iNetKeyNums == nums) {
                                strContent = strContent.substring(i + 1, j);
//                      Tools.printLog("strContentstrContentstrContent"+strContent);
                                return strContent;
                            } else {
                                iNetKeyNums += 1;
                                break;
                            }
                        }
                    }
                }
            }
        }
        return null;
    }
    //判断是否有了相关提示

    public int checkIfHaveC(String type) {

        if (contentV == null) {
            return -1;
        }
        byte temp, objtype;
        objtype = Byte.parseByte(getNetKey(type, 2));
        if (objtype == 4) {
            return -1;
        }

        for (int i = 0; i < contentV.size(); i++) {
            temp = Byte.parseByte(getNetKey((String) contentV.elementAt(i), 2));
            if (temp == objtype) {
                return i;
//                contentV.setElementAt(type, i);
//                break;
            }
        }
        return -1;
    }

    public void updateQuickOption() {
        if (MainCanvas.keyPressedLogic(Constant.GAME_KEY_NEGATIVE, false)
                || MainCanvas.keyPressedLogic(Constant.GAME_KEY_POUND, false)) {
            //提示框按键
            MainLogic.getInstance().pushState(Constant.GAME_UI_ROAM, true);
        } else if (MainCanvas.keyPressedLogic(Constant.GAME_KEY_UP, false)) {
            //提示框按键
            if (iSelectIndex > 0) {
                iSelectIndex -= 1;
            }
        } else if (MainCanvas.keyPressedLogic(Constant.GAME_KEY_DOWN, false)) {
            //提示框按键
            if (iSelectIndex < iSelectMaxIndex) {
                iSelectIndex += 1;
            }
        } else if (MainCanvas.keyPressedLogic(Constant.GAME_KEY_LEFT, false)) {
            //提示框按键
        } else if (MainCanvas.keyPressedLogic(Constant.GAME_KEY_RIGHT, false)) {
            //提示框按键
        } else if (MainCanvas.keyPressedLogic(Constant.GAME_KEY_FIRE, false) || MainCanvas.keyPressedLogic(Constant.GAME_KEY_5, false) || MainCanvas.keyPressedLogic(Constant.GAME_KEY_POSITIVE, false)) {
            //提示框按键
            byte type = Byte.parseByte(getNetKey((String) contentV.elementAt(iSelectIndex), 2));
            switch (type) {
                case 0://继续任务
                    GameBean.BY_PRE_GAME_UI_STATE = -1;
                    MainLogic.uiInterFace.npcTalk.openTaskList();
                    break;
                case 1://学新技能
                    GameBean.STR_CMD_LEARN_SKILL_KEY = getNetKeyValue((String) contentV.elementAt(iSelectIndex), 0);
                    GameBean.SH_CMD_LEARN_SKILL_POSX = Short.parseShort(getNetKeyValue((String) contentV.elementAt(iSelectIndex), 1));
                    GameBean.SH_CMD_LEARN_SKILL_POSY = Short.parseShort(getNetKeyValue((String) contentV.elementAt(iSelectIndex), 2));
                    GameBean.I_SYSTEM_MESSAGE_TYPE = 5;
                    MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "你有新的技能可学，请拜访技能商人购买新技能并使用学习，是否寻路？");
                    break;
                case 2://可点经脉
                    MainLogic.getInstance().uiEquipment.meridianschoose_index = 0;
                    MainLogic.getInstance().send(Constant.MSG_NET_CMD_OPEN_MERIDIAN, Constant.SOCKET_MSG_TYPE_LOGIC);
                    break;
                case 3://装备
                    MainLogic.getInstance().uiPackage.openBag(Constant.ITEM_COLUMN_TYPE_COMMON);
                    break;
                case 4://礼包(槽key,栏类型)
                    GameBean.L_CMD_GOODS_SLOTKEY = Long.parseLong(getNetKeyValue((String) contentV.elementAt(iSelectIndex), 0));
                    GameBean.BY_CMD_COLUMN_TYPE = Byte.parseByte(getNetKeyValue((String) contentV.elementAt(iSelectIndex), 1));
                    MainLogic.getInstance().send(Constant.MSG_NET_CMD_USE_ITEM, Constant.SOCKET_MSG_TYPE_LOGIC);
                    break;
                case 6://属性加点
//                    MainLogic.getInstance().send(Constant.MSG_NET_CMD_CHECK_PROPERTY, Constant.SOCKET_MSG_TYPE_LOGIC);
                    MainLogic.getInstance().send(Constant.MSG_NET_CMD_OPEN_PROPERTY_POINT, Constant.SOCKET_MSG_TYPE_LOGIC);
                    GameBean.b_WAIT_NET_MSG_BACK = true;
                    break;
            }
            if (iSelectIndex != 0) {
                contentV.removeElementAt(iSelectIndex);
            }
        }
    }

    public void clearQuickOption() {
        sMess = null;
        sTitle = null;
        imgArray = null;
    }
    /*************************************************倒计时宝箱******************************************************************/
    public static int POS_X = 0;
    public static int POS_Y = 0;
    public static int POS_X2 = 0;
    public static int POS_Y2 = 0;
    public static int count_index = 0;
    public static byte byBoxShowType = 1;
    //宝箱类型
    public static byte byBoxType = 1;
    public static long lBoxStartTimeSE = 0;
    public static int lBoxContinueTime = -1;
    public static String lBoxCurrentMIN = null;
    public static String lBoxCurrentSEC = null;
    public static Image imgBox = null;
    public static Image[] imgBoxborder = null;
    public static Image imgBoxNum = null;
    public static Image imgBoxNumber = null;
    public static Calendar calendar = Calendar.getInstance();

    public static void initBox() {
        try {
            if (byBoxType >= 9) {
                return;
            }
            if (imgBox == null) {
                imgBox = ResManager.getImage(Constant.RES_UI_PATH + "90000116.png", 9999);
                imgBoxborder = new Image[3];
                imgBoxborder[0] = Image.createImage(imgBox, 44, 0, 14, 14, Constant.TRANS_NONE);
                imgBoxborder[1] = Image.createImage(imgBox, 44 + 14, 0, 14, 14, Constant.TRANS_NONE);
                imgBoxborder[2] = Image.createImage(imgBox, 44 + 28, 0, 14, 14, Constant.TRANS_NONE);
            }
            if (imgBoxNum == null) {
                imgBoxNum = ResManager.getImage(Constant.RES_UI_PATH + "90000114.png", 9999);
            }
            if (imgBoxNumber == null) {
                imgBoxNumber = ResManager.getImage(Constant.RES_UI_PATH + "90000047.png", -1);
            }
            POS_X = Constant.SCREEN_WIDTH - 40;
            //#if NOKIA_S60V5
             POS_Y = 100;
            //#else
//#            POS_Y = 86;
            //#endif
            byBoxShowType = 1;
//            if(lBoxContinueTime == -1)
            lBoxContinueTime = getBoxTime(byBoxType);//获得倒计时秒数
//            else
//            lBoxContinueTime = getBoxTime(byBoxType) - lBoxContinueTime;
//            System.out.println(calendar.get(Calendar.MINUTE));
//            lBoxStartTimeSE = Calendar.getInstance().get(Calendar.MINUTE) * 60 + Calendar.getInstance().get(Calendar.SECOND);
            lBoxStartTimeSE = (System.currentTimeMillis() / 1000);
            lBoxCurrentMIN = getTime(0, (byte) 1);
            lBoxCurrentSEC = getTime(0, (byte) 2);
        } catch (Exception e) {
        }
    }

    public static void updateBox() {
        if (byBoxType >= 9) {
            return;
        }
        if (byBoxShowType == -1) {
            return;
        }

        count_index += 1;
        if (count_index % 3 == 0) {
//        lBoxCurrentMIN = getTime((Calendar.getInstance().get(Calendar.MINUTE) * 60 + Calendar.getInstance().get(Calendar.SECOND) - lBoxStartTimeSE), (byte) 1);
//        lBoxCurrentSEC = getTime((Calendar.getInstance().get(Calendar.MINUTE) * 60 + Calendar.getInstance().get(Calendar.SECOND) - lBoxStartTimeSE), (byte) 2);
            lBoxCurrentMIN = getTime(((System.currentTimeMillis()) / 1000 - lBoxStartTimeSE), (byte) 1);
            lBoxCurrentSEC = getTime(((System.currentTimeMillis()) / 1000 - lBoxStartTimeSE), (byte) 2);
            if (byBoxShowType == 1 && lBoxCurrentMIN.endsWith("00") && lBoxCurrentSEC.endsWith("00")) {
                byBoxShowType = 2;
                lBoxContinueTime = -1;
                //发送倒计时宝箱
                GameBean.BY_CMD_COUNTBOX_TYPE = QuickOption.byBoxType;
                MainLogic.getInstance().send(Constant.MSG_NET_CMD_COUNT_BOX, Constant.SOCKET_MSG_TYPE_LOGIC);
                   MainLogic.getInstance().uiEquipment.getChatInformation(100, "", (byte) 5, "您当前在线时常达到要求，神羿赠送你【倒计时宝箱】，请进入背包查看。");
//                MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "您当前在线时常达到要求，神羿赠送你【倒计时宝箱】，请进入背包查看。");
            }
        }
        if (byBoxShowType == 3) {

            if (count_index > 30) {
                count_index = 0;
                if (byBoxShowType != 1) {
                    byBoxShowType = 2;
                }
//               byBoxShowType =2;
                //测试
//             if(!MainLogic.blLogined)
//            {
//             QuickOption.initBox();
//            }
            }
        }
    }

    public static void drawBox(Graphics g) {
        if (byBoxType >= 9) {
            return;
        }
        if (byBoxShowType == -1) {
            return;
        }

        if (byBoxShowType == 1 || byBoxShowType == 2)//正常提示
        {
            g.setClip(POS_X, POS_Y, 22, 22);
            g.drawImage(imgBox, POS_X, POS_Y, 0);
            g.setClip(POS_X - 7, POS_Y + 22 + 4, 38, 9);
            g.drawImage(imgBoxNum, POS_X - 6 + 16, POS_Y + 22 + 4, 0);
            Tools.drawNumPic(g, imgBoxNumber, lBoxCurrentMIN, (byte) 2, POS_X - 6 + 2, POS_Y + 22 + 3, (byte) 2);
            Tools.drawNumPic(g, imgBoxNumber, lBoxCurrentSEC, (byte) 2, POS_X - 6 + 20, POS_Y + 22 + 3, (byte) 2);
//         System.out.println("lBoxCurrentSEC"+lBoxCurrentSEC);
            g.setClip(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
        } else if (byBoxShowType == 3)//闪烁打开提示
        {
            g.setClip(POS_X, POS_Y, 22, 22);
            g.drawImage(imgBox, POS_X - 22, POS_Y, 0);
            g.setClip(POS_X - 7, POS_Y + 22 + 4, 38, 9);
            g.drawImage(imgBoxNum, POS_X - 6 + 16 - 22, POS_Y + 22 + 4, 0);
            Tools.drawNumPic(g, imgBoxNumber, getTime((Calendar.getInstance().get(Calendar.MINUTE) * 60 + Calendar.getInstance().get(Calendar.SECOND) - lBoxStartTimeSE), (byte) 1), (byte) 2, POS_X - 6 + 2, POS_Y + 22 + 3, (byte) 2);
            Tools.drawNumPic(g, imgBoxNumber, getTime((Calendar.getInstance().get(Calendar.MINUTE) * 60 + Calendar.getInstance().get(Calendar.SECOND) - lBoxStartTimeSE), (byte) 2), (byte) 2, POS_X - 6 + 20, POS_Y + 22 + 3, (byte) 2);
            int posx = count_index % 3;
//         if(posx == 0)
            g.setClip(POS_X - 5, POS_Y - 5, 32, 32);
            {
                g.drawImage(imgBoxborder[posx], POS_X - 3, POS_Y - 3, 0);
                g.drawRegion(imgBoxborder[posx], 0, 0, 14, 14, Constant.TRANS_MIRROR, POS_X + 14 - 3, POS_Y - 3, 0); //右上角
                g.drawRegion(imgBoxborder[posx], 0, 0, 14, 14, Constant.TRANS_MIRROR_ROT180, POS_X - 3, POS_Y + 14 - 3, 0); //左下角
                g.drawRegion(imgBoxborder[posx], 0, 0, 14, 14, Constant.TRANS_ROT180, POS_X + 14 - 3, POS_Y + 14 - 3, 0); //右下角
            }
            g.setClip(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
        }
        updateBox();
    }

    public static void clearBox() {
        imgBox = null;
        imgBoxNum = null;
        byBoxShowType = -1;
    }

    public static String getTime(long countStart, byte type) {
        long time;
        if (type == 1)//获得分钟
        {
            time = ((lBoxContinueTime - countStart) / 60) < 0 ? 0 : ((lBoxContinueTime - countStart) / 60);
            return (time < 10) ? "0" + String.valueOf(time) : String.valueOf(time);
        } else if (type == 2)//获得秒
        {
            time = ((lBoxContinueTime - countStart) % 60) < 0 ? 0 : ((lBoxContinueTime - countStart) % 60);
            return (time < 10) ? "0" + String.valueOf(time) : String.valueOf(time);
        }
        return "00"; //111111111111111
    }

    public static int getBoxTime(byte type) {
        switch (type) {
            case 1:
                return 30;
            case 2:
                return 60;
            case 3:
                return 90;
            case 4:
                return 120;
            case 5:
                return 150;
            case 6:
                return 150;
            case 7:
                return 150;
            case 8:
                return 150;
        }
        return 0;
    }
}
